<template>
  <el-form
    ref="form"
    :model="form"
    label-width="100px"
    style="width: 600px; margin: 0 auto"
  >
    <el-form-item label="上级分类">
      <el-select v-model="form.parentid" placeholder="">
        <el-option :key="0" :value="0" label="请选择">请选择</el-option>
        <el-option
          :key="item.catid"
          :value="item.catid"
          :label="item.catname"
          v-for="item in category_select"
        >
          {{
            "&nbsp;&nbsp;&nbsp;&nbsp;".repeat(item.lev) +
            "┖─&nbsp;" +
            item.catname
          }}
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="分类名称">
      <el-input v-model="form.catname"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="onSubmit">提交</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
export default {
  props: [
    "parentid",
    "module",
    "add_url",
    "lst_url",
    "mod_url",
    "del_url",
    "preview_url",
  ],

  data() {
    return {
      form: {
        parentid: parseInt(this.parentid) || 0,
        catname: "",
      },
      category_select: [],
    };
  },
  methods: {
    async onSubmit() {
      try {
        let data = await this.$axios.post(this.add_url, this.form);
        if (parseInt(data.code) > 0) {
          this.$message({
            message: data.msg,
            type: "success",
            duration: 1000,
          });
          setTimeout(() => {
            location.reload();
          }, 1000);
        } else {
          this.$message.error(data.msg);
        }
      } catch (error) {
        this.$message.error(data.msg);
      }
    },
  },
  async mounted() {
    try {
      let data = await this.$axios.post(this.lst_url, {});
      if (parseInt(data.code) > 0) {
        this.category_select = data.data;
      } else {
        this.$message.error(data.msg);
      }
    } catch (error) {
      this.$message.error(error.toString());
    }
  },
};
</script>
<style scoped>
</style>
